from drf_yasg.utils import swagger_serializer_method
from rest_framework import serializers

from lab.device.models import Device
from lab.message.models import Message
from lab.user.models import ExtendedUser


class DeviceMessageSerializer(serializers.ModelSerializer):
    entity_id = serializers.SerializerMethodField()
    message_id = serializers.SerializerMethodField()
    device_name = serializers.SerializerMethodField()
    reserve_start_time = serializers.SerializerMethodField()
    reserve_end_time = serializers.SerializerMethodField()
    create_time = serializers.SerializerMethodField()
    is_read = serializers.SerializerMethodField()
    content = serializers.SerializerMethodField()

    @swagger_serializer_method(serializer_or_field=serializers.DateTimeField)
    def get_entity_id(self, obj):
        return obj.get("device").id

    @swagger_serializer_method(serializer_or_field=serializers.CharField)
    def get_device_name(self, obj):
        return obj.get("device").name

    @swagger_serializer_method(serializer_or_field=serializers.DateTimeField)
    def get_reserve_start_time(self, obj):
        return obj.get("device").reserve_start_time

    @swagger_serializer_method(serializer_or_field=serializers.DateTimeField)
    def get_reserve_end_time(self, obj):
        return obj.get("device").reserve_end_time

    @swagger_serializer_method(serializer_or_field=serializers.DateTimeField)
    def get_create_time(self, obj):
        return obj.get("device_message").create_time

    @swagger_serializer_method(serializer_or_field=serializers.IntegerField)
    def get_message_id(self, obj):
        return obj.get("device_message").id

    @swagger_serializer_method(serializer_or_field=serializers.IntegerField)
    def get_is_read(self, obj):
        return obj.get("device_user_message").is_read

    @swagger_serializer_method(serializer_or_field=serializers.CharField)
    def get_content(self, obj):
        return obj.get("device_message").content

    class Meta:
        model = Message
        fields = [
            "message_type",
            "entity_id",
            "message_id",
            "device_name",
            "reserve_start_time",
            "reserve_end_time",
            "create_time",
            "is_read",
            "content"]


class AppointmentMessageSerializer(serializers.ModelSerializer):
    entity_id = serializers.SerializerMethodField()
    order_number = serializers.SerializerMethodField()
    status = serializers.SerializerMethodField()
    device_id = serializers.SerializerMethodField()
    message_id = serializers.SerializerMethodField()
    device_name = serializers.SerializerMethodField()
    appointment_date = serializers.SerializerMethodField()
    occupied_time_slot = serializers.SerializerMethodField()
    create_time = serializers.SerializerMethodField()
    is_read = serializers.SerializerMethodField()
    content = serializers.SerializerMethodField()

    @swagger_serializer_method(serializer_or_field=serializers.DateTimeField)
    def get_entity_id(self, obj):
        return obj.get("appointment").id

    @swagger_serializer_method(serializer_or_field=serializers.CharField)
    def get_order_number(self, obj):
        return obj.get("appointment").order_number

    @swagger_serializer_method(serializer_or_field=serializers.IntegerField)
    def get_status(self, obj):
        return obj.get("appointment").status

    @swagger_serializer_method(serializer_or_field=serializers.DateTimeField)
    def get_appointment_date(self, obj):
        return obj.get("appointment").appointment_date

    @swagger_serializer_method(serializer_or_field=serializers.CharField)
    def get_occupied_time_slot(self, obj):
        return obj.get("appointment").occupied_time_slot

    @swagger_serializer_method(serializer_or_field=serializers.CharField)
    def get_user_name(self, obj):
        return ExtendedUser.objects.filter(id=obj.get("appointment").user_id).first().user.username

    @swagger_serializer_method(serializer_or_field=serializers.CharField)
    def get_device_name(self, obj):
        return obj.get("device").name

    @swagger_serializer_method(serializer_or_field=serializers.IntegerField)
    def get_device_id(self, obj):
        return Device.objects.filter(id=obj.get("device").device_id).first().id

    @swagger_serializer_method(serializer_or_field=serializers.DateTimeField)
    def get_create_time(self, obj):
        return obj.get("appointment_message").create_time

    @swagger_serializer_method(serializer_or_field=serializers.IntegerField)
    def get_message_id(self, obj):
        return obj.get("appointment_message").id

    @swagger_serializer_method(serializer_or_field=serializers.IntegerField)
    def get_is_read(self, obj):
        return obj.get("appointment_user_message").is_read

    @swagger_serializer_method(serializer_or_field=serializers.CharField)
    def get_content(self, obj):
        return obj.get("appointment_message").content

    class Meta:
        model = Message
        fields = [
            "message_type",
            "entity_id",
            "order_number",
            "status",
            "device_id",
            "message_id",
            "device_name",
            "appointment_date",
            "occupied_time_slot",
            "create_time",
            "is_read",
            "content",

        ]
